package com.tkjt.pms.modules.indexcalculator.thread;

import com.tkjt.pms.calc.KpiCalculator;
import com.tkjt.pms.common.entity.TkIndexCalculator;
import com.tkjt.pms.common.utils.CalcuConsts;
import com.tkjt.pms.common.utils.IndexCodesRepositories;
import com.tkjt.pms.modules.indexcalculator.service.TkIndexCalculatorService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CaculateThread implements Runnable{
	
	private TkIndexCalculatorService tkIndexCalculatorService;
	
	private KpiCalculator kpiCalculator;
	
	private TkIndexCalculator tkIndexCalculator;

	public CaculateThread(TkIndexCalculator tkIndexCalculator, TkIndexCalculatorService tkIndexCalculatorService, KpiCalculator kpiCalculator) {
		this.tkIndexCalculator = tkIndexCalculator;
		this.tkIndexCalculatorService = tkIndexCalculatorService;
		this.kpiCalculator = kpiCalculator;
	}
	@Override
	public void run() {
		String orgGroup = tkIndexCalculator.getIndexGroup();
		TkIndexCalculator t =tkIndexCalculatorService.getById(tkIndexCalculator.getId());
		try {
			//存入计算成功信息
			caculateDetail(orgGroup);
			t.setCalcuStatus(3);
			t.setCalcuDate(new Date());
			t.setUpdateDate(new Date());
			tkIndexCalculatorService.saveOrUpdate(t);
		} catch (Exception e) {
			//存入计算异常信息
			t.setCalcuStatus(4);
			t.setCalcuDate(new Date());
			t.setUpdateDate(new Date());
			t.setCalcuLogs(e.getMessage());
			try {
				tkIndexCalculatorService.saveOrUpdate(t);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 具体计算过程
	 * @param orgCode
	 * @throws Exception
	 */
	public void caculateDetail(String orgCode) throws Exception {
		List<String> indexCodes = new ArrayList<String>();
		//事业群和社区
		if("0".equals(orgCode)) {
			indexCodes = IndexCodesRepositories.findIndexGroupRepository(CalcuConsts.INDEX_UNIT);
			for(String indexCode : indexCodes) {
				//todo
				//kpiCalculator.doCalculator("C", indexCode, null);
			}
			indexCodes = IndexCodesRepositories.findIndexGroupRepository(CalcuConsts.INDEX_GROUP);
			for(String indexCode : indexCodes) {
				//todo
				//kpiCalculator.doCalculator("G", indexCode, null);
			}
		}
		//社区医院
		else if("2".equals(orgCode)) {
			indexCodes = IndexCodesRepositories.findIndexGroupRepository(CalcuConsts.INDEX_UNIT);
			for(String indexCode : indexCodes) {
				//todo
				//kpiCalculator.doCalculator("C", indexCode, null);
			}
		}
		//事业群
		else if("1".equals(orgCode)) {
			indexCodes = IndexCodesRepositories.findIndexGroupRepository(CalcuConsts.INDEX_GROUP);
			for(String indexCode : indexCodes) {
				//todo
				//kpiCalculator.doCalculator("G", indexCode, null);
			}
		}		
		
	}

}
